/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.open.chat.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.farsunset.cim.sdk.server.model.Message;
import com.open.chat.config.RedisUtil;
import com.open.chat.push.DefaultMessagePusher;
import com.open.chat.service.RedisSerice;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/message")
public class MessageController {

	@Resource
	private DefaultMessagePusher defaultMessagePusher;
	@Resource
	private RedisSerice redisSerice;
	@Resource
	private RedisUtil redisUtils;
	@PostMapping(value = "/send")

	public ResponseEntity<Long> send(@RequestBody Message message)  {

		message.setId(System.currentTimeMillis());

		defaultMessagePusher.push(message);

		return ResponseEntity.ok(message.getId());
	}
	@PostMapping("/login")
	public void login(@RequestParam String names) {
		/**
		 * 功能描述：从缓存中读取聊天信息
		 *
		 * @param key 缓存聊天信息的键
		 * @return 缓存中聊天信息list
		 */
			List<String> chatList = null;
			//			//判断key是否存在
			if (redisUtils.hasKey(names)) {
				//读取缓存中的聊天内容
				Jedis jedis=new Jedis();
				chatList=jedis.lrange(names,0,redisUtils.lGetListSize(names));
				for(int i=0;i<chatList.size();i++){
					  String objece=chatList.get(i);
					  JSONArray jsonArray= JSON.parseArray(objece);
					   JSONObject jsonObject1 = jsonArray.getJSONObject(1);

          			    String action=jsonObject1.get("action").toString();
						String content=jsonObject1.get("content").toString();
						Long id= (Long) jsonObject1.get("id");
						String receiver=jsonObject1.get("receiver").toString();
						String sender=jsonObject1.get("sender").toString();
						Long timestamp=(Long) jsonObject1.get("timestamp");
						String title=jsonObject1.get("title").toString();

						Message message=new Message();
						message.setAction(action);
						message.setContent(content);
						message.setId(id);
						message.setReceiver(receiver);
						message.setSender(sender);
						message.setTimestamp(timestamp);
						message.setTitle(title);
						send(message);
				}

				redisUtils.del(names);
			} else {
				System.out.println("此次解答无聊天信息");
			}


	}
}




